<?php

$blog = file_get_contents($argv[1]);

// Skip UTF-8 prefix if present.
if (substr($blog, 0, 3) == "\xEF\xBB\xBF") {
  echo substr($blog, 0, 3);
  $blog = substr($blog, - strlen($blog) + 3);
}

// This function replaces the `` blocks. By default the formatting is
// C++ language.
function filter_inline_code($code) {
  $lang = "cpp";
  $code = trim($code);
  $code = str_replace('\\"', '"', $code);
  return "<code class=\"$lang\">$code</code>";
}

function wiki_youtube_video($id) {
  return <<<EOT
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/$id&rel=0&color1=0xb1b1b1&color2=0xcfcfcf&feature=player_embedded&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed src="http://www.youtube.com/v/F0HW3Mth0Pw&rel=0&color1=0xb1b1b1&color2=0xcfcfcf&feature=player_embedded&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="425" height="344"></embed></object>
EOT;
}

// This function replaces the {{{ }}} blocks.
function filter_code($code) {
  $lang = "cpp";
  if (strpos($code, "[==========]") !== false) 
    $lang = "css";
  if (strpos($code, ".cmd") !== false) 
    $lang = "dos";
  if (strpos($code, "/EHsc") !== false) 
    $lang = "dos";

  $code = preg_replace(
    "/\&/",
    "&amp;",
    $code
  );

  $code = preg_replace(
    "/</",
    "&lt;",
    $code
  );

  $code = preg_replace(
    "/>/",
    "&gt;",
    $code
  );

  $code = str_replace('\\"', '"', $code);
  $code = trim($code);

  // Convert any spaces in the code block to '_SPC_' preventing
  // its processing by any filter later.
  $code = str_replace(" ", "^SPC^", $code);

  // Convert all '*' in the code block to '_AST_' preventing
  // its processing by any filter later.
  $code = str_replace("*", "^AST^", $code);

  // Convert all '_' in the code block to '_UND_' preventing
  // its processing by any filter later.
  $code = str_replace("_", "^UND^", $code);

  return "<pre><code class=\"$lang\">$code</code></pre>";
}

// This function trims spaces around the tag.
function trim_tag_spaces($text, $tag) {
  // Trim any spaces after the tag.
  $text = preg_replace(
    "/(<\/$tag>)\s+/s",
    "$1",
    $text
  );

  // Trim any spaces before the tag.
  $text = preg_replace(
    "/\s+(<$tag>)/s",
    "$1",
    $text
  );

  return $text;
}

// Replace {{{ }}} code blocks.
$blog = preg_replace(
  "/{{{(.+?)}}}/se",
  "filter_code('$1')",
  $blog
);

// Replace in-line `` code blocks.
$blog = preg_replace(
  "/`([^`]+)`/se",
  "filter_inline_code('$1')",
  $blog
);

$blog = preg_replace(
  "/<wiki\:video url=\"http\:\/\/www\.youtube\.com\/watch\?v=([^\"]+)\"\s*\/>/se",
  "wiki_youtube_video('$1')",
  $blog
);

// Replace _ in links to -ITA-.
$blog = preg_replace(
  "/(src|href)=\"([^\"]+)\"/se",
  "'$1' . '=\"' . str_replace('_', '-ITA-', '$2') . '\"'",
  $blog
);

// Replace http://image.url/name.(jpg|gif|png) links.
$blog = preg_replace(
  "/(http\:\/\/\S+?(jpg|gif|png))/",
  "<img src=\"\\1\"/>",
  $blog
);

// Replace [url] links.
$blog = preg_replace(
  "/\[(http\:\/\/\S+?)\]/",
  "<a href=\"\\1\">\\1</a>",
  $blog
);

// Replace [url http://image.url/name.(jpg|gif|png)] links.
$blog = preg_replace(
  "/\[(http\:\/\/[^ ]+?) (http\:\/\/\S+?(jpg|gif|png))\]/",
  "<a href=\"\\1\"><img src=\"\\2\"/></a>",
  $blog
);

// Replace [url name] links.
$blog = preg_replace(
  "/\[(http\:\/\/[^ ]+?) ([^\]]+)\]/",
  "<a href=\"\\1\">\\2</a>",
  $blog
);

// Replace *bold* blocks.
$blog = preg_replace(
  "/\*(\S+)\*/",
  "<b>$1</b>",
  $blog
);

// Replace _italic_ blocks.
$blog = preg_replace(
  "/_([^_]+?)_/",
  "<i>$1</i>",
  $blog
);

// Replace -ITA- in links to _.
$blog = preg_replace(
  "/(src|href)=\"([^\"]+)\"/se",
  "'$1' . '=\"' . str_replace('-ITA-', '_', '$2') . '\"'",
  $blog
);

// Replace '--' to &#151;.
$blog = str_replace(
  " -- ", " &#151; ", $blog
);


// Replace '------' delimiters.
$blog = preg_replace(
  "/^---+/m", "<hr size=\"1\" />", $blog
);

// Trim any spaces after </pre>.
$blog = preg_replace(
  "/(<\/pre>)\s+/s",
  "$1",
  $blog
);

// Replace bullet items to <li></li>.
$blog = preg_replace(
  "/^ \* (.+)$/m",
  "<li>$1</li>",
  $blog
);

// Trim any spaces before and before <li> and after </li>.
$blog = trim_tag_spaces($blog, "li");

// Add <ul> and </ul> to the bullet items.
$blog = preg_replace(
  '/(<li>.+<\/li>)/',
  "<ul>$1</ul>",
  $blog
);

// Trim any spaces before <ul> and after </ul>.
$blog = trim_tag_spaces($blog, "ul");

// Replace indented blocks to quotes.
$blog = preg_replace(
  '/^ +(.+?)$/m',
  "<blockquote>$1</blockquote>",
  $blog
);

// Trim any spaces before and before <blockquote> and after </blockquote>.
$blog = trim_tag_spaces($blog, "blockquote");

// Skip #labels lines.
$blog = preg_replace(
  "/^\#labels.+\$/m",
  "",
  $blog
);

// Skip #summary lines.
$blog = preg_replace(
  "/^\#summary.+\$/m",
  "",
  $blog
);

// Convert = H1 = headers to the plain text.
$blog = preg_replace(
  "/^= ([^=]+?)=/m",
  "$1",
  $blog
);

// Convert back spaces in the code blocks.
$blog = str_replace("^SPC^", " ", $blog);
// Convert back '*' symbols in the code blocks.
$blog = str_replace("^AST^", "*", $blog);
// Convert back '_' symbols in the code blocks.
$blog = str_replace("^UND^", "_", $blog);

$blog = trim($blog);

echo $blog;

?>